package com.fofia.iotweb.web.domain.service;

import cn.hutool.core.util.StrUtil;
import com.fofia.iotweb.web.domain.mappers.GatewayMapper;
import com.fofia.iotweb.web.domain.models.IotGateway;
import com.fofia.iotweb.web.dto.DataTableDto;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

@Component
public class GatewayService {

    @Inject("gateway")
    private GatewayMapper _gatewayMapper;

    /**
     * 分页列表，包含总记录数total
     * @param gatewayId
     * @param page
     * @param psize
     * @param startTime
     * @param endTime
     * @return
     */
    public DataTableDto getPageList(String gatewayId, int page, int psize, String startTime, String endTime){
        String tableName= StrUtil.format("g_{}",new  Object[]{gatewayId});
        DataTableDto  dto=new DataTableDto();
        QueryWrapper queryWrapper=QueryWrapper.create();
        queryWrapper.from(tableName);
        queryWrapper.where("upload_time > '"+startTime+"' and upload_time <'"+endTime+"'");
        queryWrapper.orderBy("upload_time desc");

        Page<IotGateway> records=_gatewayMapper.paginate(page,psize,queryWrapper);
        var list=records.getRecords();
        for(IotGateway gateway : list) {
            gateway.setGatewayId(gatewayId);
        }
        dto.setRows(list);
        dto.setTotal(records.getTotalRow());
        dto.setPageNumber(page);
        dto.setPageSize(psize);

        return dto;
    }
}
